package MANA;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * Reads a level in the same dir as the .class files</br>
 * Use like so: </br>
 * <code>Level l = new Level();</br>l.setLevel("level.txt");</br></code>
 * In the level file:</br>
 * # represents a wall</br>@ represents the player</br>+ represents a door (not yet)</br>
 * @author daan :-)
 *
 */
public class Level {
	private String level;

	/**
	 * Reads a level from the system
	 * @param file Path to the file to load
	 * @return a level, as a string
	 * @throws IOException if the file cannot be found
	 */
	public String setLevel(String file) throws IOException {
		//reads in same dir as package dir!!!!!!!!!!!!!!(src)
		file = "MANA/res/" + file;

		BufferedReader br = new BufferedReader(new FileReader(file));

		try {
			StringBuilder sb = new StringBuilder();
			String line = br.readLine();
			
			while (line != null) {
				sb.append(line);
				sb.append("\n");
				line = br.readLine();
			}
			level = sb.toString();
		} catch (IOException ioe) {
			System.err.println("IOException:");
			ioe.getMessage();
			ioe.printStackTrace();
			level = "";
		} catch (Exception e) {
			System.err.println("Error:");
			System.err.println(e.getMessage());
			level = "";
		} finally {
			br.close();
		}
		return level;
	}
}
